package xyz.haijin.shiro.model;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 返回封装对象
 * @author haijin
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    private String code;
    private String msg;

    private T result;

    private Result(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    private Result(String code, T result, String msg) {
        this.code = code;
        this.msg = msg;
        this.result = result;
    }

    public static <T> Result<T> error(){
        return new Result<T>(CommonConstant.FAIL,"操作失败");
    }

    public static <T> Result<T> error(String msg){
        return new Result<T>(CommonConstant.FAIL,msg);
    }

    public static <T> Result<T> error(String code,String msg){
        return new Result<T>(code,msg);
    }

    public static <T> Result<T> error(String code,T data,String msg){
        return new Result<T>(code,data,msg);
    }

    public static <T> Result<T> success(T data,String msg){
        return new Result<T>(CommonConstant.SUCCESS,data,msg);
    }

    public static <T> Result<T> success(String msg){
        return success(null,msg);
    }

    public static <T> Result<T> success(){
        return success(null,"操作成功");
    }

    public static <T> Result<T> success(T data){
        return success(data,"操作成功");
    }

}

